% dataFolder= '12_13_17_01';
clear all
% 
% path1='C:\Users\tomas\Documents\1. Harvard\2017 - Spring\Regehr lab rotation\Data\';
% path2=[ dataFolder(1:2) dataFolder(6:8) '\' dataFolder(1:8) '\' dataFolder '\'];
path='C:\Users\Tom�s\Desktop\temp\'

dir_to_search=[path]
formatpattern = fullfile(dir_to_search, '**/*.ibw');
dInfoNS = dir(formatpattern);
[~, idx] =sort([dInfoNS.datenum]); % sort structure indices by date of creation, to avoid 10 coming before 2.

dinfo=dInfoNS(idx); 


firingRate=[];
for  i=1:numel(dinfo)
   locs=[]
    fileName = [dinfo(i).folder '\' dinfo(i).name];
    Ibw= IBWread(fileName); %49
    dataMat(:,i)=Ibw.y;
    
    

figure;plot(Ibw.y(1:1e5))


prompt = 'set threshold for pk detection         ';
thresh = input(prompt)
close gcf


%% extract spike indices using peakfind
if thresh>0
[pks,locs]=findpeaks(Ibw.y,'minPeakprominence',thresh,'minpeakDistance',200);
else
[pks,locs]=findpeaks(Ibw.y*-1,'minPeakprominence',-thresh,'minpeakDistance',200);
end
    
   
% [pks locs]=findpeaks(Ibw.y*-1,'minPeakHeight',40,'minpeakDistance',200);
firingRate(i)=numel(locs)/(Ibw.Nsam*Ibw.dx);
    
end
time=[1:numel(Ibw.y)]*Ibw.dx;
figure;plot(time,Ibw.y)
firingRateMean=mean(firingRate(:))
fclose('all');

if numel(locs)>10
    ISI=(diff(locs)*Ibw.dx);
   CV=std(ISI)/mean(ISI)
end

fclose all